import {iconv} from '@ohos/hvigor';
import fse from 'fs-extra';
import fsExtra from 'fs-extra';
import JSON5 from 'json5';
import path from 'path';

import {buildOneHapAndCheckFile, resourcesPath} from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import {ProjectBuildOption, TestCaseFun} from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: {
    [ModuleTemplateType.SharedLibrary]: 1
  }
};

async function updateStringConfig(moduleResourcesPath: string) {
  const stringList = ['base', 'en_US', 'zh_CN'];
  for (const string of stringList) {
    const stringJsonPath = path.resolve(moduleResourcesPath, string, 'element', 'string.json');
    const stringData = await fse.readFile(stringJsonPath);
    const stringJson = JSON.parse(stringData.toString());
    stringJson['string'].push({
      'name': 'MainAbility_label',
      'value': 'Entry Label'
    });
    await fse.writeFile(stringJsonPath, JSON.stringify(stringJson, null, 2), 'utf-8');
  }
}

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();

  // 2.在工程AppScope和entry模块存在相同的string资源，例如MainAbility_label
  const scopeStringPath = path.resolve(projectRoot, 'AppScope/resources/base/element/string.json');
  const scopeBuffer = await fsExtra.readFile(scopeStringPath);
  const featureBuildProfileJson = JSON5.parse(iconv.decode(scopeBuffer, 'utf-8'));
  featureBuildProfileJson.string.push({
    name: 'MainAbility_label',
    value: '测试测试'
  });
  await fse.writeFile(scopeStringPath, JSON.stringify(featureBuildProfileJson, null, 2), 'utf-8');

  // 3.AppScope和entry的在相同限定词目录下string资源MainAbility_label相同，但是value值不相同
  await updateStringConfig(path.resolve(projectRoot, 'entry/src/main/resources'));

  // 4.AppScope和entry模块目录下存在相同的图片资源，例如icon.png
  await fsExtra.copyFile(
    path.resolve(resourcesPath, 'entry/fa/app/listAbility/api10/src/main/resources/base/media/ic_back.png'),
    path.resolve(projectRoot, 'AppScope/resources/base/media/icon.png')
  );

  // 5.在AppScope模块中的config.json文件中label引用MainAbility_label资源,icon引用icon.png图片资源
  const appJsonPath = path.resolve(projectRoot, 'AppScope/app.json5');
  const appJsonBuffer = await fsExtra.readFile(appJsonPath);
  const appJson = JSON5.parse(iconv.decode(appJsonBuffer, 'utf-8'));
  appJson.app.icon = '$media:icon';
  appJson.app.label = '$string:MainAbility_label';
  await fse.writeFile(appJsonPath, JSON.stringify(appJson, null, 2), 'utf-8');

  await buildOneHapAndCheckFile('entry')(project, taskConfig, record);
};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_Build_Stage_Resource_Priority_HOS_api10_STAGE_01')
  .description('01.hos-Stage单模块工程entry资源优先级')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.defaultOpt())
  .testFun(testFun)
  .load();